package org.abratuhi.kvngroupware.chat.mptua;

import java.util.ArrayList;

import org.abratuhi.kvngroupware.chat.manager.ChatroomManager;
import org.abratuhi.kvngroupware.chat.model.Chatroom;
import org.abratuhi.kvngroupware.network.AbstractRequestMessageProceeder;
import org.abratuhi.kvngroupware.network.Client;
import org.abratuhi.kvngroupware.network.Message;
import org.abratuhi.kvngroupware.network.MessageSender;
import org.abratuhi.kvngroupware.server.db.ServerDB;
import org.abratuhi.kvngroupware.user.manager.UserManager;
import org.abratuhi.kvngroupware.user.model.User;
import org.jdom.Element;

public class ChatroomRequestMessageProceeder extends AbstractRequestMessageProceeder{

	MessageSender sender;
	UserManager usermanager;
	ChatroomManager chatroommanager;
	ServerDB db;

	public ChatroomRequestMessageProceeder(MessageSender sender, UserManager usermanager, ChatroomManager chatroommanager, ServerDB db) {
		this.sender = sender;
		this.usermanager = usermanager;
		this.chatroommanager = chatroommanager;
		this.db = db;
	}

	@Override
	public void proceedMessage(Client client, Message message) {
		Element request = message.getRoot().getChild("request");
		String type = request.getAttributeValue("type");
		String action = request.getAttributeValue("action");
		String chatroomId = request.getAttributeValue("chatroomId");
		String chatroomTheme = request.getAttributeValue("chatroomTheme");
		String userId = request.getAttributeValue("userId");

		String[] attrNames = new String[]{"type", "action", "chatroomId",  "chatroomTheme", "userId", "status"};
		String[] attrValues= new String[]{type, action, chatroomId, chatroomTheme, userId, "ok"};
		String text = new String();
		Element[] content = new Element[0];
		Message responseMessage;

		if(type.equals("chatroom")){
			if(action.equals("create")){
				Chatroom chatroom = chatroommanager.get(chatroomId);
				if(chatroom == null){
					chatroom = new Chatroom(chatroomId, chatroomTheme);
					chatroommanager.put(chatroom.getId(), chatroom);
				}
				else{
					attrValues[4] = "chatroomId_found";
				}
			}
			else if(action.equals("get")){
				ArrayList<Element> eChatrooms = new ArrayList<Element>();
				for(int i=0; i<chatroommanager.size(); i++){
					Element eChatroom = chatroommanager.get(chatroommanager.getkeys()[i]).toElement();
					eChatrooms.add(eChatroom);
				}
				content = eChatrooms.toArray(new Element[eChatrooms.size()]);
			}
			else if(action.equals("delete")){
				Chatroom chatroom = chatroommanager.get(chatroomId);
				if(chatroom != null){
					chatroommanager.remove(chatroom.getId());
				}
				else{
					attrValues[4] = "chatroomId_notfound";
				}
			}
			else if(action.equals("join")){
				User user = usermanager.get(userId);
				Chatroom chatroom = chatroommanager.get(chatroomId);
				if(chatroom != null && user != null && !chatroom.containsMember(user.getId())){
					chatroom.addMember(user.getId());
				}
				else if(chatroom == null){
					attrValues[4] = "chatroomId_notfound";
				}
				else if(user == null){
					attrValues[4] = "userId_notfound";
				}
				else if(chatroom.containsMember(user.getId())){
					attrValues[4] = "userId_found";
				}
			}
			else if(action.equals("leave")){
				User user = usermanager.get(userId);
				Chatroom chatroom = chatroommanager.get(chatroomId);

				if(chatroom != null && user != null){
					chatroom.removeMember(userId);
				}
				else if(chatroom == null){
					attrValues[4] = "chatroomId_notfound";
				}
				else if(user == null){
					attrValues[4] = "userId_notfound";
				}

				// TODO: consider, whether chatrooms must be deleted after all participating members have left it
				/*if(chatroom != null && user != null && chatroom.getMembers().size() == 0){
				this.removeChatroom(chatroomId);
				String[] attrNames2 = new String[]{"type", "action", "chatroomId",  "userId", "status"};
				String[] attrValues2 = new String[]{type, "delete", chatroomId, userId, "ok"};
				String text2 = new String();
				Element[] content2 = new Element[0];
				Message responseMessage2 = createMessage(attrNames2, attrValues2, text2, content2);
				this.sendMessage(responseMessage2);
			}*/
			}
			else if(action.equals("send")){
				User user = usermanager.get(userId);
				Chatroom chatroom = chatroommanager.get(chatroomId);
				if(chatroom != null && user != null){
					text = request.getText();
				}
				else if(chatroom == null){
					attrValues[4] = "chatroomId_notfound";
				}
				else if(user == null){
					attrValues[4] = "userId_notfound";
				}
			}

			responseMessage = sender.createMessage(attrNames, attrValues, text, content);
			sender.sendMessage(responseMessage);
		}
	}

}
